package com.uyaer.wheel.plus
{
	import com.uyaer.wheel.common.Common;
	import com.uyaer.wheel.events.EventManager;
	import com.uyaer.wheel.events.EventType;
	import com.uyaer.wheel.tick.TimerManager;
	
	import so.cuo.anes.admob.AdEvent;
	import so.cuo.anes.admob.AdSize;
	import so.cuo.anes.admob.Admob;

	/**
	 *广告 
	 * @author hc
	 * 
	 */	
	public class AD
	{
		private static var mInstance:AD;
		private var adID:String = "a151df89d92710e";
		private var isDebug:Boolean = false;
		private var isHome:Boolean = false;
		
		private var admob:Admob;
		public function AD()
		{
			admob = Admob.getInstance();
		}
		
		public static function get me():AD{
			return mInstance ||= new AD();
		}
		
		public function showHomeAD():void{
			if(admob.isSupported){
				if(isHome){
					admob.addToStage((Common.W-admob.getAdSize().width)*0.5,Common.H*0.2);
				}else{
					isHome = true;
					admob.createADView(AdSize.IAB_MRECT,adID);
					admob.addToStage(10,Common.H*0.25);
					admob.load(isDebug);
					admob.dispatcher.addEventListener(AdEvent.onDismissScreen,clickAd);
					admob.dispatcher.addEventListener(AdEvent.onLeaveApplication,clickAd);
				}
			}
		}
		
		private function clickAd(e:AdEvent):void{
			EventManager.dispatchEvent(EventType.AD_CLICKED);
		}
		
		public function showGameAD():void{
			if(admob.isSupported){
				isHome = false;
				admob.createADView(AdSize.BANNER,adID);
				admob.addToStage(0,Common.H*0.85);
				admob.load(isDebug);
				admob.dispatcher.addEventListener(AdEvent.onReceiveAd,onLoadOver);
				admob.dispatcher.addEventListener(AdEvent.onFailedToReceiveAd,showGameAD);
			}
		}
		
		private function onLoadOver(e:AdEvent):void{
			TimerManager.addDelay(function():void{
				admob.addToStage((Common.W-admob.getAdSize().width)*0.5,Common.H-admob.getAdSize().height*1.5);
			},100);
		}
		
		public function hideAD():void{
			admob.dispatcher.removeEventListener(AdEvent.onReceiveAd,onLoadOver);
			admob.dispatcher.removeEventListener(AdEvent.onFailedToReceiveAd,showGameAD);
			admob.removeFromStage();
		}
	}
}